//
//  TYH_LoginMsg.m
//
//  Created by 啊  on 16/4/24
//  Copyright (c) 2016 __MyCompanyName__. All rights reserved.
//

#import "TYH_LoginMsg.h"


NSString *const kTYH_LoginMsgPId = @"pId";
NSString *const kTYH_LoginMsgNewUser = @"newUser";
NSString *const kTYH_LoginMsgTtl = @"ttl";
NSString *const kTYH_LoginMsgToken = @"token";
NSString *const kTYH_LoginMsgTime = @"time";


@interface TYH_LoginMsg ()

- (id)objectOrNilForKey:(id)aKey fromDictionary:(NSDictionary *)dict;

@end

@implementation TYH_LoginMsg

@synthesize pId = _pId;
@synthesize newUser = _newUser;
@synthesize ttl = _ttl;
@synthesize token = _token;
@synthesize time = _time;


+ (instancetype)modelObjectWithDictionary:(NSDictionary *)dict
{
    return [[self alloc] initWithDictionary:dict];
}

- (instancetype)initWithDictionary:(NSDictionary *)dict
{
    self = [super init];
    
    // This check serves to make sure that a non-NSDictionary object
    // passed into the model class doesn't break the parsing.
    if(self && [dict isKindOfClass:[NSDictionary class]]) {
            self.pId = [self objectOrNilForKey:kTYH_LoginMsgPId fromDictionary:dict];
            self.newUser = [[self objectOrNilForKey:kTYH_LoginMsgNewUser fromDictionary:dict] doubleValue];
            self.ttl = [[self objectOrNilForKey:kTYH_LoginMsgTtl fromDictionary:dict] doubleValue];
            self.token = [self objectOrNilForKey:kTYH_LoginMsgToken fromDictionary:dict];
            self.time = [self objectOrNilForKey:kTYH_LoginMsgTime fromDictionary:dict];

    }
    
    return self;
    
}

- (NSDictionary *)dictionaryRepresentation
{
    NSMutableDictionary *mutableDict = [NSMutableDictionary dictionary];
    [mutableDict setValue:self.pId forKey:kTYH_LoginMsgPId];
    [mutableDict setValue:[NSNumber numberWithDouble:self.newUser] forKey:kTYH_LoginMsgNewUser];
    [mutableDict setValue:[NSNumber numberWithDouble:self.ttl] forKey:kTYH_LoginMsgTtl];
    [mutableDict setValue:self.token forKey:kTYH_LoginMsgToken];
    [mutableDict setValue:self.time forKey:kTYH_LoginMsgTime];

    return [NSDictionary dictionaryWithDictionary:mutableDict];
}

- (NSString *)description 
{
    return [NSString stringWithFormat:@"%@", [self dictionaryRepresentation]];
}

#pragma mark - Helper Method
- (id)objectOrNilForKey:(id)aKey fromDictionary:(NSDictionary *)dict
{
    id object = [dict objectForKey:aKey];
    return [object isEqual:[NSNull null]] ? nil : object;
}


#pragma mark - NSCoding Methods

- (id)initWithCoder:(NSCoder *)aDecoder
{
    self = [super init];

    self.pId = [aDecoder decodeObjectForKey:kTYH_LoginMsgPId];
    self.newUser = [aDecoder decodeDoubleForKey:kTYH_LoginMsgNewUser];
    self.ttl = [aDecoder decodeDoubleForKey:kTYH_LoginMsgTtl];
    self.token = [aDecoder decodeObjectForKey:kTYH_LoginMsgToken];
    self.time = [aDecoder decodeObjectForKey:kTYH_LoginMsgTime];
    return self;
}

- (void)encodeWithCoder:(NSCoder *)aCoder
{

    [aCoder encodeObject:_pId forKey:kTYH_LoginMsgPId];
    [aCoder encodeDouble:_newUser forKey:kTYH_LoginMsgNewUser];
    [aCoder encodeDouble:_ttl forKey:kTYH_LoginMsgTtl];
    [aCoder encodeObject:_token forKey:kTYH_LoginMsgToken];
    [aCoder encodeObject:_time forKey:kTYH_LoginMsgTime];
}

- (id)copyWithZone:(NSZone *)zone
{
    TYH_LoginMsg *copy = [[TYH_LoginMsg alloc] init];
    
    if (copy) {

        copy.pId = [self.pId copyWithZone:zone];
        copy.newUser = self.newUser;
        copy.ttl = self.ttl;
        copy.token = [self.token copyWithZone:zone];
        copy.time = [self.time copyWithZone:zone];
    }
    
    return copy;
}


@end
